//
// Created by LiuYou on 2021/8/3.
//

#include <cstdlib>
#include <unistd.h>
#include <cstdio>
#include <sys/wait.h>


#define BUF_SIZE 1024


int main() {
    int pipeFD[2];
    if (pipe( pipeFD ) < 0) {
        perror( "pipe()" );
        exit( 1 );
    }

    pid_t pid = fork();
    if (pid < 0) {
        perror( "fork()" );
        exit( 1 );
    }

    if (pid == 0) {
        close( pipeFD[1] );
        char buf[BUF_SIZE];
        ssize_t len = read( pipeFD[0], buf, BUF_SIZE );
        write( 1, buf, len );
        close( pipeFD[0] );
    } else {
        close( pipeFD[0] );
        write( pipeFD[1], "Hello!\n", 7 );
        close( pipeFD[1] );
        wait( nullptr );
        exit( 0 );
    }


}